package com.gn.controller;

import com.gn.compile.PL0;
import com.gn.domain.Code;
import com.gn.domain.MyPair;
import com.gn.domain.PL0Code;
import com.gn.domain.Result;
import com.gn.service.PL0Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/word")
public class WordController {
    @Autowired
    private PL0Service pl0Service;

    @PostMapping
    public Result insert(@RequestBody PL0Code pl0_code) {
        // PL/0代码解析
        String code = pl0_code.getCode();
        PL0.getSource(code);
        List<MyPair<Integer, String> > scannerOuts = PL0.lex.getScannerOuts();
        // 写入数据库
        boolean flag = pl0Service.save(pl0_code);
        return new Result(scannerOuts, flag ? Code.SAVE_OK : Code.SAVE_ERR, "");
    }

    @GetMapping
    public Result selectAll() {
        List<PL0Code> pl0Codes = pl0Service.select();
        Integer code = !pl0Codes.isEmpty() ? Code.GET_OK : Code.GET_ERR;
        String message = !pl0Codes.isEmpty() ? "" : "用户名或密码错误！";
        return new Result(pl0Codes, code, message);
    }
}
